package com.letao.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letao.server.dto.BatchOrderDetailDTO;
import com.letao.server.dto.PageDTO;
import com.letao.server.dto.Result;
import com.letao.server.entity.OrderDetail;
import com.letao.server.mapper.OrderDetailMapper;
import com.letao.server.service.IBaseCategoryService;
import com.letao.server.service.IOrderDetailService;
import com.letao.server.service.ISkuService;
import com.letao.server.service.ISpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private ISkuService skuService;

    @Resource
    private ISpuService spuService;

    @Resource
    private IBaseCategoryService baseCategoryService;

    /**
     * 分页查询订单明细
     *
     * @param orderId  订单的Id
     * @param page     页码
     * @param pageSize 页大小
     * @return 返回的结果
     */
    @Override
    public Result getPageInfo(Long orderId, Integer page, Integer pageSize) {
        IPage<OrderDetail> orderPage = new Page<>(page, pageSize);
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        IPage<OrderDetail> orderDetailIPage = orderDetailMapper.selectPage(orderPage, wrapper);

        List<BatchOrderDetailDTO> batchOrderDetailDTOList = new ArrayList<>();
        for (OrderDetail record : orderDetailIPage.getRecords()) {
            BatchOrderDetailDTO batchOrderDetailDTO = BeanUtil.copyProperties(record, BatchOrderDetailDTO.class);
            batchOrderDetailDTO.setSkuName(skuService.getById(record.getSkuId()).getSkuName());

            batchOrderDetailDTO.setSkuImage(skuService.getById(record.getSkuId()).getHasImage());

            batchOrderDetailDTO.setSpuName(spuService.getById(record.getSpuId()).getSpuName());
            batchOrderDetailDTO.setSpuImage(spuService.getById(record.getSpuId()).getSpuImage());
            batchOrderDetailDTO.setOneCategoryId(spuService.getById(record.getSpuId()).getOneCategoryId());
            batchOrderDetailDTO.setOneCategoryName(baseCategoryService.getById(spuService.getById(record.getSpuId()).getOneCategoryId()).getName());
            batchOrderDetailDTO.setTwoCategoryId(spuService.getById(record.getSpuId()).getTwoCategoryId());
            batchOrderDetailDTO.setTwoCategoryName(baseCategoryService.getById(spuService.getById(record.getSpuId()).getTwoCategoryId()).getName());

            batchOrderDetailDTO.setPrice(skuService.getById(record.getSkuId()).getPrice());

            batchOrderDetailDTOList.add(batchOrderDetailDTO);
        }


        //构造返回结果
        PageDTO<BatchOrderDetailDTO> batchOrderDetailPageDTO = new PageDTO<>();
        batchOrderDetailPageDTO.setRows(batchOrderDetailDTOList);
        batchOrderDetailPageDTO.setTotalSize(orderDetailIPage.getTotal());
        batchOrderDetailPageDTO.setPages(orderDetailIPage.getPages());
        batchOrderDetailPageDTO.setPageSize(orderDetailIPage.getSize());
        batchOrderDetailPageDTO.setCurrentPage(orderDetailIPage.getCurrent());
        log.info("管理员/用户查询了订单号={}的详细商品信息", orderId);
        return Result.success(batchOrderDetailPageDTO);
    }
}
